package com.yuhuihui.common.utils.excel.read.convert.impl;

import com.yuhuihui.common.utils.ConverterUtil;
import com.yuhuihui.common.utils.LocalDateUtil;
import com.yuhuihui.common.utils.excel.read.convert.AbstractCellTypeConvert;
import com.yuhuihui.common.utils.excel.read.convert.CellTypeConvertParameter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 数值型类型转换
 *
 * @author yuhh
 * @date 2022-08-22 16:22:59
 * @see AbstractCellTypeConvert
 */
public class NumericCellTypeConvertImpl implements AbstractCellTypeConvert {

	/** 格式列表 */
	private final List<Short> FORMAT_LIST = Arrays.asList((short)14, (short)31, (short)32, (short)57, (short)58);

	@Override
	public Object cellTypeConvert(CellTypeConvertParameter parameter) throws ParseException {
		Cell cell = parameter.getCell();
		short format = cell.getCellStyle().getDataFormat(); 
		// 处理自定义日期格式
		 if(FORMAT_LIST.contains(format)){
             double value = cell.getNumericCellValue();
			 Date date = DateUtil.getJavaDate(value);
			 return ConverterUtil.convert(parameter.getParameterType(), LocalDateUtil.getFormatDateTime(date));
         }  
		 else if (DateUtil.isCellDateFormatted(cell)) {
			// 时间
			 Date date = DateUtil.getJavaDate(cell.getNumericCellValue());
			 return ConverterUtil.convert(parameter.getParameterType(), LocalDateUtil.getFormatDateTime(date));
        } else {
        	// 数字
			 return ConverterUtil.convert(parameter.getParameterType(), cell.getNumericCellValue());
        }
	}

	@Override
	public int getTypeCode() {
		return CellType.NUMERIC.ordinal();
	}

}
